package com.zxx.xin.shorthand.ui.widget;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.zxx.xin.shorthand.R;
import com.zxx.xin.shorthand.database.bean.Daily;
import com.zxx.xin.shorthand.database.DatabaseHelper;
import com.zxx.xin.shorthand.helper.DateHelper;
import com.zxx.xin.shorthand.ui.activity.DailyReadActivity;

import java.util.HashMap;
import java.util.List;

/**
 * Created by xin on 2018/3/18.
 */

public class DailyAdapter extends RecyclerView.Adapter {

    private List<Daily> mList;

    private Context mContext;

    private HashMap<Integer, String> mYearAndMonthMap = new HashMap<>();

    public DailyAdapter(List<Daily> list, Context context){
        mList = list;
        resetYearAndMonthMap();
        mContext = context;
    }

    private View.OnClickListener mClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            DailyReadActivity.startPage(mContext, mList.get((Integer) v.getTag()));
        }
    };

    private View.OnLongClickListener mLongClickListener = new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(final View v) {
            AlertDialog dialog = new AlertDialog.Builder(mContext).setTitle(R.string.delete_tip).setPositiveButton(R.string.sure, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Daily daily = mList.get((Integer) v.getTag());
                    mList.remove((int)v.getTag());
                    notifyDataChanged();
                    DatabaseHelper.getInstance().deleteDaily(daily);
                }
            }).setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            }).create();
            dialog.show();
            return false;
        }
    };

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == 0){
            View view = LayoutInflater.from(mContext).inflate(R.layout.daily_list_day_item, parent, false);
            return new DailyViewHolder(view);
        } else {
            View view = LayoutInflater.from(mContext).inflate(R.layout.daily_list_year_month_day_item, parent, false);
            return new DailyWithTimeViewHolder(view);
        }
    }

    public void notifyDataChanged(){
        resetYearAndMonthMap();
        super.notifyDataSetChanged();
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (getItemViewType(position) == 0){
            DailyViewHolder dailyViewHolder = (DailyViewHolder) holder;
            dailyViewHolder.mTitle.setText(mList.get(position).getMTitle());
            dailyViewHolder.mDay.setText(DateHelper.getChineseDayInMillis(mList.get(position).getMCreateTime()));
            dailyViewHolder.itemView.setTag(position);
            dailyViewHolder.itemView.setOnClickListener(mClickListener);
            dailyViewHolder.itemView.setOnLongClickListener(mLongClickListener);
        } else {
            DailyWithTimeViewHolder dailyWithTimeViewHolder = (DailyWithTimeViewHolder) holder;
            dailyWithTimeViewHolder.mTitle.setText(mList.get(position).getMTitle());
            dailyWithTimeViewHolder.mDay.setText(DateHelper.getChineseDayInMillis(mList.get(position).getMCreateTime()));
            dailyWithTimeViewHolder.mTime.setText(mYearAndMonthMap.get(position));
            dailyWithTimeViewHolder.mContent.setTag(position);
            dailyWithTimeViewHolder.mContent.setOnClickListener(mClickListener);
            dailyWithTimeViewHolder.mContent.setOnLongClickListener(mLongClickListener);
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (mYearAndMonthMap.containsKey(position)){
            return 1;
        } else {
            return 0;
        }
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    private void resetYearAndMonthMap(){
        mYearAndMonthMap.clear();
        if (mList.size() > 0){
            String lastYearAndMonth = DateHelper.getChineseYearAndMonthInMillis(mList.get(0).getMCreateTime());
            mYearAndMonthMap.put(0, lastYearAndMonth);
            for (int i = 1; i < mList.size(); i++){
                String currentYearAndMonth = DateHelper.getChineseYearAndMonthInMillis(mList.get(i).getMCreateTime());
                if (!currentYearAndMonth.equals(lastYearAndMonth)){
                    mYearAndMonthMap.put(i, currentYearAndMonth);
                    lastYearAndMonth = currentYearAndMonth;
                }
            }
        }
    }



    class DailyViewHolder extends RecyclerView.ViewHolder{
        public VerticalTextView mDay;
        public  VerticalTextView mTitle;
        public DailyViewHolder(View itemView) {
            super(itemView);
            mDay = (VerticalTextView) itemView.findViewById(R.id.day);
            mTitle = (VerticalTextView) itemView.findViewById(R.id.title);
        }
    }

    class DailyWithTimeViewHolder extends RecyclerView.ViewHolder{
        public VerticalTextView mDay;
        public VerticalTextView mTitle;
        public VerticalTextView mTime;
        public View mContent;
        public DailyWithTimeViewHolder(View itemView) {
            super(itemView);
            mDay = (VerticalTextView) itemView.findViewById(R.id.day);
            mTitle = (VerticalTextView) itemView.findViewById(R.id.title);
            mTime = (VerticalTextView) itemView.findViewById(R.id.year_and_month);
            mContent = itemView.findViewById(R.id.item_content);
        }
    }
}
